package view.component.cost
{
import flash.display.MovieClip;

import framework.core.AssetsManager;
import framework.proxy.ComponentProxy;
import framework.utils.TextUtil;
import framework.view.components.graphics.Asset;

import values.item.INumbericItemValue;
import values.item.ItemTemplateValue;
import values.item.ItemTemplateValueList;

/**
 * CostItemProxy
 * @author jliang
 * 
 */
public class CostGroupItemProxy extends ComponentProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function CostGroupItemProxy()
	{
		super();
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	protected var asset:Asset;
	//==========================================================================
	//  Properties
	//==========================================================================
	private var _data:Object;

	public function get data():Object
	{
		return _data;
	}

	public function set data(value:Object):void
	{
		_data = value;
		
		skin.visible = true;
		
		var num:Number = 1;
		if (data is INumbericItemValue)
		{
			num = INumbericItemValue(data).num;
		}
		TextUtil.setText(skin["label"], String(num));
		if (num <= 1)
		{
			skin["label"].visible = false;
		}
		else
		{
			skin["label"].visible = true;
		}
		
		if (asset)
		{			
			if (asset.parent)
			{
				asset.parent.removeChild(asset);
			}
			asset.dispose();
			asset = null;
		}
		
		var template:ItemTemplateValue = ItemTemplateValueList.getByKey(data.key);
		asset = AssetsManager.loadAsset(template.getIconPathByKey("tiny"));
		skin["holder"].addChild(asset);
	}
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		skin.visible = false;
	}	
	
	override public function dispose():void		
	{
		if (asset)
		{
			if (asset.parent)
			{
				asset.parent.removeChild(asset);
			}
			asset.dispose();
		}
		super.dispose();
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function clear():void
	{
		if (asset)
		{
			if (asset.parent)
			{
				asset.parent.removeChild(asset);
			}
			asset.dispose();
		}
		asset = null;
		TextUtil.setText(skin["label"], "");
	}
}
}